﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions.Problems
{
    /*
     * NOTE: This is a more difficult version of problem 114.

A row measuring n units in length has red blocks with a minimum length of m units placed on it, such that any two red blocks (which are allowed to be different lengths) are separated by at least one black square.

Let the fill-count function, F(m, n), represent the number of ways that a row can be filled.

For example, F(3, 29) = 673135 and F(3, 30) = 1089155.

That is, for m = 3, it can be seen that n = 30 is the smallest value for which the fill-count function first exceeds one million.

In the same way, for m = 10, it can be verified that F(10, 56) = 880711 and F(10, 57) = 1148904, so n = 57 is the least value for which the fill-count function first exceeds one million.

For m = 50, find the least value of n for which the fill-count function first exceeds one million.

     * */
    class Problem115 : IProblem
    {
        public string Calculate()
        {
            int m = 50;

            int l = m;


            long count = 0;
            while (count < 1000000)
            {
                count = C(++l, m);
            }
            
            return l.ToString();
        }

        public long C(int l, int m)
        {
            long count = 1;
            for (int n = 1; n <= (l + 1) / (m + 1); n++)
            {
                for (int k = 0; k <= l - (m + 1) * n + 1; k++)
                {
                    count += CommonFunctions.CalculateBinome(l - m * n + 1 - k, n) * CommonFunctions.CalculateBinome(k + n - 1, n - 1);
                }
            }

            return count;
        }
    }
}
